import request from "../utils/request"
import store from "../store"
import { graphql_request } from "../utils/some_useful"

export function login(user_name, password) {
  var data = { user_name: user_name, password: password }
  return request({
    url: "/login",
    method: "post",
    data,
    headers: {
      "Content-Type": "application/json",
    },
  })
}

export function logout() {
  store.commit("deleteToken")
}

export function regist(user_name, password) {
  var data = `mutation{
    addUser(input: [
        {
            name:"${user_name}", 
            password:"${password}",
            role:[{
                name: "normal",
            }]
        }
      ]) {
            user {
                name
                role {
                  name
                }
            }
        }
  }
  `
  return request(graphql_request(data))
}

export function getUserInfo(user_name) {
  var data = `query {
    queryUser(filter:{
      name: {eq:"${user_name}"}
    }) {
      name,
      role {
        name
      }
    }
  }
  `
  return request(graphql_request(data))
}

export function getOthersInfo() {
  var data = `query{
    queryUser(filter: {
      name: {eq: "others"}
    }) {
      name,
      role {
        name
      }
    }
  }`
  return request(graphql_request(data))
}
